/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.da.controllerBeans;

import edu.chl.da.ejb.core.Flight;
import edu.chl.da.ejb.ctrl.IManageFlight;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 * Controller bean managing listing, getting and removal events of flights
 * @author Erik
 */
@ManagedBean(name = "ManageFlightsBean")
@SessionScoped
public class ManageFlightsBean {

    private List<Flight> flights;
    private Flight selectedFlight;


    private boolean success = false;
    private String errorMsg;


    @EJB
    IManageFlight imf;

    /** Creates a new instance of ManageFlightsBean */
    public ManageFlightsBean() {

    }

    public void loadFlights() {
        this.flights = imf.getAllFlights();
    }
    public String deleteFlight() {
        Flight f = imf.getFlight(selectedFlight.getFlightPK());
        boolean b = imf.removeFlight(selectedFlight);
        if(b) {
            flights.remove(f);
            return "/admin/manageFlights.jsf";
        }
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("Gick ej att radera resan, kontrollera eventuella flygningar"));
        return null;
    }
        public String navigate() {
        if (success) {
            return "/admin/manageFlights.jsf";
        } else {
            // Adding a failure message. Possible to display all
            // messages using <h:messages> (ending s!!)
            // or just target a specific control using <h:message> (no s!).
            FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(errorMsg));
           return null; //Same page;

        }
    }

    public List<Flight> getFlights() {
        return flights;
    }

    public void setFlights(List<Flight> flights) {
        this.flights = flights;
    }
    public Flight getSelectedFlight() {
        return selectedFlight;
    }

    public void setSelectedFlight(Flight selectedFlight) {
        this.selectedFlight = selectedFlight;
    }
}
